package com.itcast.spring6.ioc.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 通过接口回调方式注入(xxxAware,这里测试BeanFactoryAware 和 ApplicationContextAware)
 * @author zhangtian105
 * @date 2023/4/28
 * @apiNote
 */
@Slf4j
public class BeanAwareTest implements BeanFactoryAware, ApplicationContextAware {
    private static BeanFactory beanFactory ;
    private static ApplicationContext applicationContext ;

    public static void main(String[] args) {
        // 创建容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext() ;
        // 注册Configuration Class（配置类）
        applicationContext.register(BeanAwareTest.class);
        // 启动应用上下文
        applicationContext.refresh();

        log.info("beanFactory == applicationContext.getBeanFactory() ? {}", beanFactory == applicationContext.getBeanFactory()) ;
        log.info("BeanAwareTest.applicationContext == applicationContext ? {}", BeanAwareTest.applicationContext == applicationContext) ;

        // 关闭spring应用上下文
        applicationContext.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        BeanAwareTest.beanFactory = beanFactory ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanAwareTest.applicationContext = applicationContext ;
    }
}
